const config = require('../../../config/config.local');
const amazonMws = require('amazon-mws')(config.AWSAccessKeyId, config.AWSSecretKey);
const isProduction = process.env.NODE_ENV === 'production';
const jsonflatten = require('pt-core-common').util.jsonflatten;

const getEligibleShippingServices = {};
getEligibleShippingServices.generateOption = function(seller_id, mws_auth_token, shipmentRequestDetails, shippingOfferingFilter=null){
  let baseOption = {
    'Version': '2015-06-01',
    'Action': 'CreateShipment',
    'SellerId': seller_id,
    'MWSAuthToken': mws_auth_token,
    // 'AmazonOrderId.Id.1': orderId
    // 'ASINList.ASIN.1': 'B07RP44QPT'
  };

  const option = Object.assign(baseOption,
    jsonflatten.flatten(shipmentRequestDetails, null, baseIndex=1),
    jsonflatten.flatten(shippingOfferingFilter, null, baseIndex=1));
  return option;
};

getEligibleShippingServices._ = function(options) {
  if (!isProduction) {
    return amazonMws.merchantFulfillment.search(options);
  } else {
    console.error("bot not implemented!")
  }
};

getEligibleShippingServices.getEligibleShippingServices = function(seller_id, mws_auth_token, shipmentRequestDetails, shippingOfferingFilter=null) {
  return this._(this.generateOption(seller_id, mws_auth_token, shipmentRequestDetails, shippingOfferingFilter));
};

module.exports = getEligibleShippingServices;